/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/4/17 22:31
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.seckill.service.impl;

import com.google.common.collect.Lists;
import com.hyacinth.seckill.common.constants.Constant;
import com.hyacinth.seckill.entity.Goods;
import com.hyacinth.seckill.entity.Order;
import com.hyacinth.seckill.repository.GoodsRepository;
import com.hyacinth.seckill.repository.OrderRepository;
import com.hyacinth.seckill.service.SeckillService;
import com.hyacinth.service.BaseService;
import com.hyacinth.wapper.RetValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 秒杀服务
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@Service
@Transactional
public class SeckillServiceImpl extends BaseService implements SeckillService {

    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private OrderRepository orderRepository;

    @Transactional
    @Override
    public RetValue reset() {
        Date NOW = new Date();
        // 删除商品表
        goodsRepository.deleteAll();
        // 插入商品记录
        Goods goods1 = new Goods("1000元秒杀iphone8", 100, NOW, NOW, NOW);
        Goods goods2 = new Goods("500元秒杀ipad2", 100, NOW, NOW, NOW);
        Goods goods3 = new Goods("300元秒杀小米4", 100, NOW, NOW, NOW);
        Goods goods4 = new Goods("200元秒杀红米note", 100, NOW, NOW, NOW);
        ArrayList<Goods> goods = Lists.newArrayList(goods1, goods2, goods3, goods4);
        List<Goods> list = goodsRepository.saveAll(goods);
        // 删除订单表
        orderRepository.deleteAll();
        return RetValue.getSuccessReturnValue();
    }

    @Transactional //添加事务不会有超卖的情况
    @Override
    public RetValue startSeckill(long goodsId, long userId) {
        // 校验库存
        Goods goods = goodsRepository.getOne(goodsId);
        if (goods == null) {
            return RetValue.getErrorReturnValue("秒杀商品不存在");
        }
        Integer goodsNum = goods.getNumber();
        if (goodsNum == null || goodsNum <= 0) {
            return RetValue.getErrorReturnValue("秒杀商品数量不足");
        }
        // 扣库存
        goods.setNumber(goodsNum - 1);
        goodsRepository.save(goods);
        // 创建订单
        Order order = new Order();
        order.setGoodsId(goodsId);
        order.setUserId(userId);
        order.setState(Constant.orderStatus_03);
        order.setCreateTime(new Date());
        orderRepository.save(order);
        // 支付
        return RetValue.getSuccessReturnValue();
    }

    @Override
    public RetValue startSeckillLock(long goodsId, long userId) {
        return null;
    }

    @Override
    public RetValue startSeckillAopLock(long goodsId, long userId) {
        return null;
    }

    @Override
    public RetValue startSeckillDBPCC_ONE(long goodsId, long userId) {
        return null;
    }

    @Override
    public RetValue startSeckillDBPCC_TWO(long goodsId, long userId) {
        return null;
    }

    @Override
    public RetValue startSeckillDBOCC(long goodsId, long userId, long number) {
        return null;
    }

    @Override
    public RetValue startSeckillTemplate(long goodsId, long userId, long number) {
        return null;
    }
}
